package com.itheima.service.impl;

import com.itheima.controller.DeptController;
import com.itheima.exception.DeptNotEmptyException;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    private static final Logger log = LoggerFactory.getLogger(DeptServiceImpl.class);


    /**
     * 查询所有部门
     */
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;


    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        int empCount = empMapper.getEmpCount(id);
        if(empCount > 0){
            throw new DeptNotEmptyException();
        }else{
            deptMapper.deleteById(id);
        }

    }

    @Override
    public void add(Dept dept) {
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateTime(now);
        dept.setUpdateTime(now);
        deptMapper.add(dept);
    }

    @Override
    public void updateById(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }

    @Override
    public Dept searchById(Integer id) {
        Dept dept = deptMapper.searchById(id);
        return dept;
    }
}
